<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.commons.Constants"%>
<%@ page import="com.scalar.chiptrack.users.User"%>
<%@ page import="com.scalar.chiptrack.users.Functionality"%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>

<chiptrack:checkLogon sourcePage="/releaseInfoSetup.do?action=releaseInfoSetup" />
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>
<html>
    <HEAD>
    <TITLE>Scalarsoft - ChipTrac</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252">
    <LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
    <script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
    <script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
    <SCRIPT language="JavaScript">
    var errors;
    var chiptracErrors;
    var chiptracInfo;
    var form;

    function init()
    {
        errors = document.getElementById("errors");
        chiptracErrors = document.getElementById("chiptracErrors");
        chiptracInfo = document.getElementById("chiptracInfo");
        form = document.releaseInfoSetupForm;
    }

    function loadReleaseInfoSetup()
    {
        var processType = form.processType.value;
        var vendorId    = form.vendorId.value;
        var category    = form.category.value;

        if( vendorId != '-1' && ( processType == '0' || processType == '-1' ) )
        {
            chiptracErrors.innerHTML = '';
            chiptracInfo.innerHTML = '';
            errors.innerHTML = "<li> Please select a Process Type";
            return false;
        }
        if( category != '-1' && ( vendorId == '0' || vendorId == '-1' ) )
        {
            chiptracErrors.innerHTML = '';
            chiptracInfo.innerHTML = '';
            errors.innerHTML = "<li> Please select a Vendor";
            form.category.selectedIndex = 0;
            return false;
        }
        form.action = form.action+"?action=load";
        form.submit();
    }

    function resetCategory()
    {
        form.category.selectedIndex = 0;
        loadReleaseInfoSetup();
    }

    function resetVendorAndReleaseType()
    {
        if( ( typeof form.vendorId != "undefined" ) && ( typeof form.category != 'undefined' ) )
        {
            form.vendorId.selectedIndex     = 0;
            form.category.selectedIndex  = 0;
            loadReleaseInfoSetup();
        }
    }

    function loadvendors()
    {
        var processType = form.processType.value;
        if( processType == '0' || processType == '-1')
        {
            chiptracErrors.innerHTML = '';
            chiptracInfo.innerHTML = '';
            errors.innerHTML = "<li> Please select a Process Type";
            return false;
        }
        form.action = form.action+"?action=LoadVendors";
        form.submit();
    }

    function saveReleaseInfoSetup()
    {
        var processType = form.processType.value;
        if( processType == '0' || processType == '-1')
        {
            errors.innerHTML = "<li> Please select a Process Type";
            return;
        }
        /*var customerNumber = form.customerNumber.value;
        if( trim( customerNumber ) == '' )
        {
            errors.innerHTML = "<li> Please enter Customer Number";
            form.customerNumber.focus();
            return;
        }*/
        /*var ftpUrl = form.ftpUrl.value;
        if( trim( ftpUrl ) == '' )
        {
            errors.innerHTML = "<li> Please enter FTP URL";
            form.ftpUrl.focus();
            return;
        }
        var directory = form.directory.value;
        if( trim( directory ) == '' )
        {
            errors.innerHTML = "<li> Please enter Directory";
            form.directory.focus();
            return;
        }
        var userName = form.userName.value;
        if( trim( userName ) == '' )
        {
            errors.innerHTML = "<li> Please enter User Name";
            form.userName.focus();
            return;
        }
        var password = form.password.value;
        if( trim( password ) == '' )
        {
            errors.innerHTML = "<li> Please enter Password";
            form.password.focus();
            return;
        }*/

        if( processType == 'Assembly')
        {
            var maxFabLotsForAssemblyRelease = form.maxLots.value;
            if(!isInteger(maxFabLotsForAssemblyRelease))
            {
                errors.innerHTML = "<li> Please enter valid value for Max FAB lots for Assembly Release";
                form.maxLots.select();
                return;
            }
            else if(maxFabLotsForAssemblyRelease == 0)
            {
                errors.innerHTML = "<li> Please enter valid value for Max FAB lots for Assembly Release";
                form.maxLots.select();
                return;
            }
        }

        var contactPersonsCount = form.contactPersonsCount.value;
        var flag = false;
        for( var i = 0; i <= contactPersonsCount; i++)
        {
            var cName = form['contactPersonsList['+i+'].name'].value;
            var cEmail = form['contactPersonsList['+i+'].email'].value;
            if( trim( cName ) != '' && trim( cEmail ) == '' )
            {
                errors.innerHTML = "<li> Please enter Email for Contact Person Name : " + cName;
                form['contactPersonsList['+i+'].email'].focus();
                return;
            }
            else if( trim( cName ) == '' && trim( cEmail ) != '' )
            {
                errors.innerHTML = "<li> Please enter Contact Person Name for Email : " + cEmail;
                form['contactPersonsList['+i+'].name'].focus();
                return;
            }
            else if( trim( cName ) != '' )
            {
                var isEmailInvalid = !checkEmail(form['contactPersonsList['+i+'].email']);
                if( isEmailInvalid )
                {
                    form['contactPersonsList['+i+'].email'].select();
                    errors.innerHTML = "<li> Please enter valid Email for Contact Person Name : " + cName;
                    return;
                }
                else
                {
                    flag = true;
                }
            }
        }
        if( !flag )
        {
            errors.innerHTML = "<li> Please enter atleast one Contact Person's Name and Email";
            return;

        }
        if( contactPersonsCount >= 0)
        {
            for( var idx = 0; idx <= contactPersonsCount; idx ++ )
            {
                var sContactName = form['contactPersonsList['+idx+'].name'].value;
                if( trim( sContactName ) != "" && checkForReplicaInContactPersonNames(sContactName) > 1 )
                {
                    errors.innerHTML = "<li> Contact Person Name : " + sContactName + ", is repeated" ;
                    return;
                }
            }
        }
        form.action = form.action+"?action=save";
        form.submit();
    }


	function checkEmail( email )
	{
        var hypen    = email.value.indexOf('-');
        var uscore   = email.value.indexOf('_');

        if( email.value != '' )
	    {
            if ( ( uscore == 0 ) || ( hypen == 0 ) )
            {
                return false;
            }

            if( !isValidEMailId (email) )
            {
                return false;
            }
         }
		return true;
	}

    function checkForReplicaInContactPersonNames( sName )
    {
        var contactPersonsCount = form.contactPersonsCount.value;
        var iHitCount = 0;
        if( contactPersonsCount >= 0)
        {
            for( var idx = 0; idx <= contactPersonsCount; idx ++ )
            {
                var sCpName = form['contactPersonsList['+idx+'].name'].value;
                if( trim(sName).toUpperCase() == trim(sCpName).toUpperCase())
                {
                    iHitCount++;
                }
            }
        }
        return iHitCount;
    }

    function addContactPerson()
    {
        form.action = form.action+"?action=addmore";
        form.submit();
    }

    function addMore( addMoreType )
    {
        form.action = form.action + "?action=addmore&addMoreType=" + addMoreType;
        form.submit();
    }

    function reset()
    {
        form.reset();
    }

    function checkDefault( chkboxCtrl )
    {
        var contactPersonsCount = form.contactPersonsCount.value;
        var iHitCount = 0;
        var isChecked = chkboxCtrl.checked;
        if( contactPersonsCount >= 0)
        {
            for( var idx = 0; idx <= contactPersonsCount; idx ++ )
            {
                form['contactPersonsList['+idx+'].primary'].checked = false;
            }
        }
        chkboxCtrl.checked = isChecked;
    }

</script>

</HEAD>
<BODY bgColor="#ffffff" leftMargin="0" topMargin="0" marginheight="0" marginwidth="0" onLoad="init()">
    <!-- Top Header Start-->
    <%@ include file="/jsp/includes/header.jsp" %>
    <!-- Top Header End-->
    <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
      <TBODY >
      <TR>
        <TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

        <!-- Side Navigation start -->
        <%@ include file="/jsp/operations/releaseMenu.jsp" %></TD>
        <!-- end of the side navigations -->

        <TD bgcolor="#ffffff">
        <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
        <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
        <TD valign="top" width="100%">

        <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
        <BR>
        <!-- from here your body starts -->
        <html:form action="/releaseInfoSetup" name="releaseInfoSetupForm" type="com.scalar.chiptrack.operations.release.forms.ReleaseInfoSetupForm">
            <TABLE width='95%' align=right cellpadding='3' cellspacing='2' border='0' bordercolor='black'>
                <tr><td colspan='4' class="NArialW" align=center>Release Info Setup</td></tr>
                <logic:equal name="showMessage" value="TRUE">
                <tr><td colspan='4' align='left'><font class='NArialS' color='blue'><B><%=Constants.NO_MODIFY_RIGHTS %></B></font></td></tr>
                </logic:equal>
                <tr><td colspan='4' align="left" class='NArialL'><FONT id="errors" color='FF0000'></font>&nbsp;</td></tr>
                <tr><td colspan='4' id='chiptracErrors'><chiptrack:errors styleClass="NArialL"/></td></tr>
                <tr><td colspan='4' id='chiptracInfo'><chiptrack:info styleClass="NArialL"/></td></tr>
                <tr>
                    <td class='NArialL' width='20%'> Process Type :</td>
                    <td class="NArialL" width='25%'>
                        <html:select property="processType" name="releaseInfoSetupForm" styleClass="txfield"  onchange="loadvendors()"  style="width:170px">
                        <html:option value="-1">--Select Process Type--</html:option>
                        <html:options collection="processTypes" property="value" labelProperty="label"/>
                        </html:select>
                    </td>
                    <td width='20%'>&nbsp;</td>
                    <td width='35%'>&nbsp;</td>
                </tr>
                <logic:present name="vendorList" >
                <tr>
                    <td class="NArialL"> Vendor :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:select property="vendorId" name="releaseInfoSetupForm" styleClass="txfield"  onchange="javascript:resetCategory();"  style="width:170px">
                        <html:option value="-1">--Select Vendor--</html:option>
                        <html:options collection="vendorList" property="value" labelProperty="label"/>
                        </html:select>
                    </td>
                    <td class='NArialL'><logic:present name="categoryList" > Category :</logic:present></td>
                    <td class="NArialL">
                        <logic:present name="categoryList" >
                        <html:select property="category" name="releaseInfoSetupForm" styleClass="txfield"  onchange="javascript:loadReleaseInfoSetup()" style="width:170px">
	                        <html:option value="-1">--Select Category--</html:option>
							<html:options name="categoryList"/>
                        </html:select>
                        </logic:present>
                    </td>
                </tr>
                <tr><td colspan='4'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></td></tr>
                <logic:equal value="true" property="displayForm" name="releaseInfoSetupForm">
                <tr>
                    <td class='NArialL'> Customer Number :<!--<font color="RED" size="1"><B>*</B></font>--></td>
                    <td class="NArialL">
                        <html:text property="customerNumber" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50"/>
                    </td>
                    <td class='NArialL'> PO Number :</td>
                    <td class="NArialL">
                        <html:text property="poNumber" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50"/>
                    </td>
                </tr>
                <logic:equal value="Assembly" property="processType" name="releaseInfoSetupForm">
                <tr>
                    <td class='NArialL'>&nbsp;</td>
                    <td class="NArialL">&nbsp;</td>
                    <td class='NArialL'> Max FAB lots for Assm Rel :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:text property="maxLots" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50" onkeypress="javascript:allowInteger()"/>
                    </td>
                </tr>
                </logic:equal>
                <tr>
                    <td class='NArialL'> FTP URL :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:text property="ftpUrl" name="releaseInfoSetupForm" styleClass="txfield" maxlength="200"/>
                    </td>
                    <td class='NArialL'> Directory :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:text property="directory" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td class='NArialL'> User Name :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:text property="userName" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50"/>
                    </td>
                    <td class='NArialL'> Password :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:text property="password" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td class='NArialL'> FTP Connection Mode :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                        <html:select property="connectionMode" name="releaseInfoSetupForm" styleClass="txfield">
                        <html:option value="ACTIVE">ACTIVE</html:option>
                        <html:option value="PASV">PASV</html:option>
                        </html:select>
                    </td>
                   <%-- <logic:equal value="Assembly" property="processType" name="releaseInfoSetupForm">
                    <td class='NArialL'> Max FAB lots for Assm Rel :<font color="RED" size="1"><B>*</B></font></td>
                    <td class="NArialL">
                   <html:text property="maxLots" name="releaseInfoSetupForm" styleClass="txfield" maxlength="50" onkeypress="javascript:allowInteger()"/>

                    </td>
                    </logic:equal>  --%>
                </tr>
                <tr><td colspan='4'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></td></tr>
                <tr>
                    <td colspan='4' width="50%" valign="top">
                        <table border="0" width="50%" align="left" cellpadding="3" cellspacing="2">
                            <tr>
                                <td class="NArialL" colspan="2"><bean:message key="label.operation.release.internalContactPersons"/>:<font color="RED" size="1"><B>*</B></font></td>
                            </tr>
                            <tr>
                                <td width="40%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Name</font></font></td>
                                <td width="50%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">EMail</font></font></td>
                                <td width="10%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Primary</font></font></td>
                            </tr>
                            <nested:iterate name="releaseInfoSetupForm" property="contactPersonsList" id="contactPerson" type="com.scalar.chiptrack.operations.release.ContactPerson" indexId="contactPersonsCount" >
                            <TR>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:text property="name" name="contactPerson" styleClass="txfield" size="25" maxlength="50"/>
                                    </font>
                                </td>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:text property="email" name="contactPerson" styleClass="txfield" size="40" maxlength="50"/>
                                    </font>
                                </td>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:checkbox property="primary" name="contactPerson" styleClass="txfield" onclick="checkDefault(this)"/>
                                    </font>
                                </td>
                            </tr>
                            </nested:iterate>
                            <input type='hidden' name='contactPersonsCount' value="<bean:write name='contactPersonsCount'/>"/>
                            <tr>
                                <td class="NArialL" align='right' colspan='3'><a href="javascript:addMore('contactPersons')"><img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border=0 onmouseover="window.status='Click here to add More';return true" onmouseout="window.status='';return true" title=""></a></TD>
                            </tr>
                        </table>
                    </td>
                </tr>
                <%--<tr>
                    <td colspan='4' width="50%" valign="top">
                        <table border="0" width="50%" align="left" cellpadding="3" cellspacing="2">
                            <tr>
                                <td class="NArialL" colspan="2">Marking :<font color="RED" size="1"><B>*</B></font></td>
                            </tr>
                            <tr>
                                <td width='40%' align="center" bgcolor="#993333"><font class="NArialL" color="#FFFFFF">&nbsp;</font></td>
                                <td width='60%' align="center" bgcolor="#993333"><font class="NArialL" color="#FFFFFF">&nbsp;</font></td>
                            </tr>
                            <tr>
                                <td align="center" bgcolor="#DEE1E2"><font class="NArialL" color="#FFFFFF">First Line</font></td>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:text property="markingFirstLine" name="releaseInfoSetupForm" styleClass="txfield" size="40" maxlength="50"/>
                                    </font>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" bgcolor="#DEE1E2"><font class="NArialL" color="#FFFFFF">Second Line</font></td>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:text property="markingSecondLine" name="releaseInfoSetupForm" styleClass="txfield" size="40" maxlength="50"/>
                                    </font>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" bgcolor="#DEE1E2"><font class="NArialL" color="#FFFFFF">Third Line</font></td>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:text property="markingThirdLine" name="releaseInfoSetupForm" styleClass="txfield" size="40" maxlength="50"/>
                                    </font>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" bgcolor="#DEE1E2"><font class="NArialL" color="#FFFFFF">Fourth Line</font></td>
                                <td align="center" bgcolor="#DEE1E2">
                                    <font class="NArialL" color="#FFFFFF">
                                        <nested:text property="markingFourthLine" name="releaseInfoSetupForm" styleClass="txfield" size="40" maxlength="50"/>
                                    </font>
                                </td>
                            </tr>
                            <tr>
                                 <td align="center" bgcolor="#DEE1E2"><font class="NArialL" color="#FFFFFF">Fifth Line</font></td>
                                 <td align="center" bgcolor="#DEE1E2">
                                     <font class="NArialL" color="#FFFFFF">
                                         <nested:text property="markingFifthLine" name="releaseInfoSetupForm" styleClass="txfield" size="40" maxlength="50"/>
                                     </font>
                                 </td>
                             </tr>
                            <tr>
                                 <td align="center" bgcolor="#DEE1E2"><font class="NArialL" color="#FFFFFF">Sixth Line</font></td>
                                 <td align="center" bgcolor="#DEE1E2">
                                     <font class="NArialL" color="#FFFFFF">
                                         <nested:text property="markingSixthLine" name="releaseInfoSetupForm" styleClass="txfield" size="40" maxlength="50"/>
                                     </font>
                                 </td>
                            </tr>
                        </table>
                    </td>
                </tr>--%>
                <tr><td colspan='4'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></td></tr>
                <tr>
                    <td colspan='4' width="100%" valign="top" align="left">
                        <table border="0" width="55%" align="left">
                            <tr>
                                <td width="100%" valign="top" align="left">
                                    <table border="0" width="100%" align="left" cellpadding="3" cellspacing="2">
                                        <tr>
                                            <td class="NArialL" colspan="3">Vendor Contact Persons:</td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Select</font></font></td>
                                            <td width="30%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Name</font></font></td>
                                            <td width="60%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">EMail</font></font></td>
                                        </tr>
                                        <nested:iterate name="releaseInfoSetupForm" property="vendorContactPersonsList" id="contactPerson" type="com.scalar.chiptrack.operations.release.ContactPerson" indexId="contactPersonsCount" >
                                        <tr>
                                            <td align="center" bgcolor="#DEE1E2">
                                                <font class="NArialL" color="#FFFFFF">
                                                    <nested:checkbox property="selected" name="contactPerson" />
                                                </font>
                                            </TD>
                                            <td align="center" bgcolor="#DEE1E2">
                                                <font class="NArialL" color="#FFFFFF">
                                                    <nested:write property="name" name="contactPerson" />
                                                </font>
                                            </TD>
                                            <td align="center" bgcolor="#DEE1E2">
                                                <font class="NArialL" color="#FFFFFF">
                                                    <nested:write property="email" name="contactPerson" />
                                                </font>
                                            </TD>
                                        </tr>
                                        </nested:iterate>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr><td colspan='4'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></td></tr>
                <tr>
                    <td colspan='4' align=center>
                        <chiptrack:restrictedLink functionality='<%=Functionality.RELEASE_INFO_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:saveReleaseInfoSetup();" onmouseover="window.status='Click here to save';return true" onmouseout="window.status='';return true">
                        <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" title=""></chiptrack:restrictedLink>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:reset()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17"  onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset" ></a>
                    </td>
                </tr>
                 </logic:equal>
                 </logic:present>
            </TABLE>
        </html:form>
    </td>
    </tr>
    </TABLE>
        <!-- Footer starts here -->
        <%@ include file="/jsp/includes/footer.jsp" %>
        <!-- footer end here -->
    </body>
</html>
